//
//  UrbanRegistTextFild.m
//  UrbanManagement
//
//  Created by Can on 18/4/9.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanRegistTextFild.h"

@interface UrbanRegistTextFild()<UITextFieldDelegate>
{
    NSInteger _maxNum;
}

@end

@implementation UrbanRegistTextFild

- (instancetype)initWithFrame:(CGRect)frame lableName:(NSString *)lableName placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType maxNum:(NSInteger)maxNum type:(UrbanRegistTextFildType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, SCREENW - 30*screenW_6, 50*screenW_6);
        
        [self creatViewWithLableName:lableName placeholder:placeholder keyboardType:keyboardType type:type];
        if (maxNum > 0) {
            _maxNum = maxNum;
        }
    }
    return self;
}

-(void)creatViewWithLableName:(NSString *)lableName placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType type:(UrbanRegistTextFildType)type
{
    UILabel *lableTitle = [TomamaUI createLabel:CGRectMake(0, 0, 85*screenW_6, self.frame.size.height) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:Color(51, 51, 51) text:lableName];
    [self addSubview:lableTitle];
    lableTitle.center = CGPointMake(lableTitle.center.x, self.frame.size.height/2);
    
    CGFloat length = 0;
    if (type == UrbanRegistTextFildPassWordType) {
        UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - 40*screenW_6, 0,40*screenW_6,40*screenW_6) targ:self sel:@selector(hiddenText:) titleColor:nil font:nil title:nil image:@"eye_off" backGroundImage:nil];
        [btn setImage:[UIImage imageNamed:@"eye_on"] forState:UIControlStateSelected];
        [self addSubview:btn];
        btn.center =CGPointMake(btn.center.x, lableTitle.center.y);
        length = 22*screenW_6;
        //18 ＊ 12
        [btn setImageEdgeInsets:UIEdgeInsetsMake(14*screenW_6,18*screenW_6, 14*screenW_6,4*screenW_6)];
    }else if (type == UrbanRegistTextFildMsgType){
        _btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - 40*screenW_6, 0, 40*screenW_6, 15*screenW_6) targ:self sel:@selector(sendMsg:) titleColor:Color(21, 121, 242) font:[UIFont systemFontOfSize:15*screenW_6] title:@"发送" image:nil backGroundImage:nil];
        [self addSubview:_btn];
        _btn.center =CGPointMake(_btn.center.x, lableTitle.center.y);
        length = 22*screenW_6;
    }else if (type == UrbanRegistTextFildBranchOfficeType){
        _btn = [TomamaUI createButton:0 Frame:CGRectMake(CGRectGetMaxX(lableTitle.frame) + 20*screenW_6, 0, self.frame.size.width - (CGRectGetMaxX(lableTitle.frame) + 20*screenW_6), self.frame.size.height) targ:self sel:@selector(goDepartment:) titleColor:Color(51, 51, 51) font:[UIFont systemFontOfSize:15*screenW_6] title:placeholder image:nil backGroundImage:nil];
        [self addSubview:_btn];
        _btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _btn.center =CGPointMake(_btn.center.x, lableTitle.center.y);
        
        UIImageView *imageV = [TomamaUI createImageView:CGRectMake(self.frame.size.width - 10*screenW_6, 0, 7*screenW_6, 12*screenW_6) backGroundImageV:@"right_arrow"];
        [self addSubview:imageV];
        imageV.center = CGPointMake(imageV.center.x, self.frame.size.height/2);
        
        length = 22*screenW_6;
    }
    
    
    if (type != UrbanRegistTextFildBranchOfficeType) {
        UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lableTitle.frame) + 20*screenW_6, 0, self.frame.size.width - CGRectGetMaxX(lableTitle.frame) - 35*screenW_6 - length,40*screenW_6)];
        textF.center =CGPointMake(textF.center.x, lableTitle.center.y);
        textF.font = [UIFont systemFontOfSize:15*screenW_6];
        textF.textColor = Color(153, 153, 153);
        textF.placeholder = placeholder;
        if (type == UrbanRegistTextFildPassWordType) {
            textF.secureTextEntry = YES;
        }
        [textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        if (keyboardType) {
            textF.keyboardType = keyboardType;
        }
        if (type == UrbanRegistTextFildPassWordType) {
            textF.secureTextEntry = YES;
        }
        textF.tag = 100;
        textF.delegate = self;
        [self addSubview:textF];
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1)];
    lineView.backgroundColor = Color(247, 247, 247);
    [self addSubview:lineView];
}

-(void)textFieldDidChange:(UITextField *)textField
{
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position && _maxNum) {
        if (textField.text.length > _maxNum) {
            textField.text = [textField.text substringToIndex:_maxNum];
        }
    }
    if (_UrbanRegistTextFildblock) {
        _UrbanRegistTextFildblock(textField.text);
    }
}

-(void)hiddenText:(UIButton *)btn
{
    btn.selected = !btn.selected;
    UITextField *textF = [self viewWithTag:100];
    textF.secureTextEntry = !btn.selected;
}

-(void)sendMsg:(UIButton *)btn
{
    
}

-(void)goDepartment:(UIButton *)btn
{
    
}

@end
